package com.zklh.leetcode.other.problem24;
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {

    public ListNode swapPairs(ListNode head) {
        ListNode result;
        if(head != null && head.next != null){
            result = head.next;
        }
        // 上个节点
        ListNode lastNode = null;
        // 两个一组节点的第一个节点
        ListNode firstNode = head;
        // 两个一组节点的第二个节点
        ListNode secondNode = null;
        while(firstNode != null && (secondNode = firstNode.next) != null){
            // 表示当前是头结点
            if(lastNode != null){
                lastNode.next = secondNode;
            }
            firstNode.next = secondNode.next;
            secondNode.next = firstNode;
            lastNode = firstNode;
            firstNode = firstNode.next;
        }
        return head;
    }
}